package cn.redsoft.turingapi.common.util;

import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Consts {
	private static final Logger log = LoggerFactory.getLogger(Consts.class);

	// const
	public static String TURING_API_CHARACTER_ENCODING = "UTF-8";

	// config properties
	public static String TURING_API_KEY;
	public static String TURING_API_REQUEST_ADDRESS;

	static {
		Properties p = new Properties();
		try {
			log.info("解析配置文件 turngapi.properties:");
			p.load(Consts.class.getClassLoader().getResourceAsStream(
					"turngapi.properties"));
			TURING_API_KEY = p.getProperty("turingapi.key");
			if (TURING_API_KEY == null) {
				log.error("\t未读取到 【turingapi.key】 配置 .");
			} else {
				log.info("\tturingapi.key={}", TURING_API_KEY);
			}
			TURING_API_REQUEST_ADDRESS = p
					.getProperty("turingapi.request.address");
			if (TURING_API_REQUEST_ADDRESS == null) {
				TURING_API_REQUEST_ADDRESS = "http://www.tuling123.com/openapi/api";
				log.warn("\t未读取到 【turingapi.request.address】 配置,使用默认值 : {}",
						TURING_API_REQUEST_ADDRESS);
			} else {
				log.info("\tturingapi.request.address={}",
						TURING_API_REQUEST_ADDRESS);
			}
		} catch (NullPointerException e) {
			log.error("未检测到配置文件 turngapi.properties .");
			e.printStackTrace();
		} catch (IOException e) {
			try {
				throw e;
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		log.info("解析 turngapi.properties 完毕 .");
	}
}
